/**
 * 
 */
package fr.upvm.lem3.vm3d.ui.swing.progressbar;

import java.awt.Color;
import java.awt.Dimension;
import java.io.Serializable;
import java.util.EventListener;

import javax.swing.BorderFactory;
import javax.swing.JProgressBar;
import javax.swing.border.Border;
import javax.swing.plaf.basic.BasicProgressBarUI;

import com.sun.java.swing.plaf.motif.MotifBorders.BevelBorder;

import fr.upvm.lem3.vm3d.services.parser.FileParsingListener;

/**
 *******************************************************************
 *MyProgressBar.java
 *17.02.2012
 *@author Guy Nougouapi
 *******************************************************************
 *
 *******************************************************************
 */
public class MyProgressBar extends JProgressBar implements Serializable, FileParsingListener{
	/**
	 * 
	 */
	private static final long serialVersionUID = 5152554885490138916L;
	
	/**
	 * string message for painting the progressBat
	 */
	private String message;	
	
	/**
	 * 
	 * @param minValue
	 * @param maxValue
	 */
	public MyProgressBar(int maxValue, Border border, Color fgCol, final Color sbgCol, final Color sfgCol){
		super();
		this.setMinimum(0);						//Initialize the progressBar with value between 0 and the value given to it when creating
		this.setMaximum(maxValue);
		this.setForeground(fgCol);
		if( border == null){
			this.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.LIGHT_GRAY));
		}
        this.setUI(new BasicProgressBarUI() {
            protected Color getSelectionBackground() { return sbgCol; }
            protected Color getSelectionForeground() { return sfgCol; }
          });
        
	}
	
	/**
	 * 
	 * @param message
	 * @param indeterminate
	 */
	public MyProgressBar(String message, boolean indeterminate ){
		this(100,null,Color.green,Color.black, Color.black);
		if(indeterminate){
			this.setString(message);
		}
		this.setMessage(message);
	}
	/**
	 * 
	 * @param maxValue
	 */
	public MyProgressBar(int maxValue){
		this(maxValue,null,Color.green,Color.black, Color.black);
	}
	/**
	 * 
	 * @param addValue
	 * @param newMessage
	 */
	public void updateValue(int addValue, String newMessage){
		this.setStringPainted(true);
		this.setIndeterminate(true);
		int value = (int)(this.getValue() + Math.rint(addValue));
		if( value >= this.getMaximum()){
			value = this.getMaximum();
			this.setString(newMessage);
			this.setMessage(newMessage);
			this.setIndeterminate(false);
		}
		this.setValue(value);
	}
	
	/**
	 * 
	 * @param addValue
	 */
	public void updateValue(int addValue){
		this.setStringPainted(true);
		int value = this.getValue() + addValue;
		if( value >= this.getMaximum()){
			value = this.getMaximum();
		}
		this.setValue(value);
	}
	
	/**
	 * @return the message
	 */
	public String getMessage() {
		return message;
	}
	/**
	 * @param message the message to set
	 */
	public void setMessage(String message) {
		this.message = message;
	}

	public void init() {
		this.setValue(this.getMinimum());
//		this.setString("");
		this.setStringPainted(false);
		
	}

	@Override
	public void valueChanged(int newValue) {
		this.updateValue(newValue);
		
	}
	
}

//  bar.setString("Operation in progress");
//  step.addActionListener(new ActionListener() {
//    public void actionPerformed(ActionEvent e) {
//  	   bar.setStringPainted(true);
//  	    bar.setIndeterminate(true);
//      int value = (int) (bar.getValue() +20);
////      System.out.println(Math.);
//      if (value > bar.getMaximum()) {
//        value = bar.getMaximum();
//        bar.setString("Done!");
//        bar.setIndeterminate(false);
//      }
//      bar.setValue(value);
//    }
//  });

